package com.leyou.sh.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.sh.common.dto.PageDTO;
import com.leyou.sh.item.dto.SkuDTO;
import com.leyou.sh.item.dto.SpecParamDTO;
import com.leyou.sh.item.dto.SpuDTO;
import com.leyou.sh.item.dto.SpuDetailDTO;
import com.leyou.sh.item.entity.SpuDetail;
import com.leyou.sh.item.service.SkuService;
import com.leyou.sh.item.service.SpecParamService;
import com.leyou.sh.item.service.SpuDetailService;
import com.leyou.sh.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author balisong_fcs
 * @date 2020/12/29 11:37
 * @desc
 */
@RestController
@RequestMapping("/goods")
public class SpuController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService detailService;
    @Autowired
    private SpecParamService paramService;
    /**
     *@desc 分页查询spu表,部分字段不传值
     *@params
     *@return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage (
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ){
        return ResponseEntity.ok(this.spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable));
    }
    /**
     *@desc   表维护,某些特殊字段的注意设置维护
     *@params 新增商品组/商品组描述/商品细节
     *@return
     */
    @PostMapping
    public ResponseEntity<Void> saveSpu(
            @RequestBody SpuDTO spuDTO
            ){
        this.spuService.saveSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 更新商品
     * 前端参数封装为spudto传回接收
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(
            @RequestBody SpuDTO spuDTO
    ){
        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据Skuids 查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(
            @RequestParam("ids") List<Long> ids
    ) {
        return ResponseEntity.ok(this.skuService.querySkuByIds(ids));
    }

    /**
     * 根据spuid查询sku集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(
            @RequestParam("id") Long id
    ) {
        return ResponseEntity.ok(this.skuService.querySkuBySpuId(id));
    }

    /**
     * 根据spuid 查询spudetail信息
     * 一对一
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuid(
            @RequestParam("id") Long id
    ) {

        return ResponseEntity.ok(this.detailService.querySpuDetailBySpuid(id));
    }

    /**
     * 根据spuid查询spu的所有规格参数值
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamValue(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false)Boolean searching
    ) {
        return ResponseEntity.ok(this.spuService.querySpecParamValue(id,searching));
    }

    /**
     * 修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateGoods(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "saleable") Boolean  saleable
    ){
        this.spuService.updateSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuid 查询spuId查询slu.spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryById(
            @PathVariable("id") Long id
            ){
        return ResponseEntity.ok(this.spuService.queryById(id));
    }

    /**
     * 根据spuId查询spu
     * @param spid
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spid){
        return ResponseEntity.ok(this.spuService.querySpuById(spid));
    }

    /**
     * 扣减库存
     *
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long,Integer> cartMap) {
        this.skuService.deductStock(cartMap);
        return ResponseEntity.ok().build();
    }
}
